﻿(function($) {
    $.widget("ui.rioCalendar", {
        options : {
            //date : '1990-01-01'
        },
        _create : function() {
            var $self = this;
            var element = this.element;
            var yearSelector = this.yearSelector = element.find('.bs-year');
            var monthSelector = this.monthSelector = element.find('.bs-month');
            var daySelector = this.daySelector = element.find('.bs-day');
            var monthDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
            
            yearSelector.bind('mouseenter', function(event) {
                $(this).addClass('bs-hover');
                $(this).find('.bs-selected').css('zIndex', 100);
                $(this).find('.bs-list').show();
            }).bind('mouseleave', function(event) {
                $(this).removeClass('bs-hover');
                $(this).find('.bs-selected').css('zIndex', 1);
                $(this).find('.bs-list').hide();
            });
            yearSelector.find('.bs-item a').each(function(index, item) {
                $(item).bind('click.rio', function(event) {
                    var year = $(item).attr('value');
                    yearSelector.find('.bs-selected').attr('value', year);
                    yearSelector.find('.bs-selected').html(year);
                    yearSelector.find('.bs-list').hide();
                    // 如果月份还没有选定，引导选择月份
                    if(monthSelector.find('.bs-selected').attr('value') == -1){
                        monthSelector.mouseenter();
                    } else {
                        daySelector.mouseenter();
                    }
                });
            });
            monthSelector.bind('mouseenter', function(event) {
                if(yearSelector.find('.bs-selected').attr('value') == -1){
                    monthSelector.mouseleave();
                    yearSelector.mouseenter();
                } else {
                    $(this).addClass('bs-hover');
                    $(this).find('.bs-selected').css('zIndex', 100);
                    $(this).find('.bs-list').show();
                    $(this).find('.bs-list').css('left', 0-yearSelector.find('.bs-selected').position().left);
                    yearSelector.mouseleave();
                }
            }).bind('mouseleave', function(event){
                $(this).removeClass('bs-hover');
                $(this).find('.bs-selected').css('zIndex', 1);
                $(this).find('.bs-list').hide();
                yearSelector.mouseleave();
            });
            monthSelector.find('.bs-item a').each(function(index, item) {
                $(item).bind('click.rio', function(event) {
                    var month = $(item).attr('value');
                    monthSelector.find('.bs-selected').attr('value', month);
                    monthSelector.find('.bs-selected').html(month);
                    monthSelector.find('.bs-list').hide();
                    // 如果年份还没有选定，引导选择年份
                    if(yearSelector.find('.bs-selected').attr('value') == -1){
                        yearSelector.mouseenter();
                    } else {
                        daySelector.mouseenter();
                    }
                });
            });
            daySelector.bind('mouseenter', function(event){
                daySelector.addClass('bs-hover');
                daySelector.find('.bs-selected').css('zIndex', 100);
                daySelector.find('.bs-list').show();
                daySelector.find('.bs-list').css('left', 0-yearSelector.find('.bs-selected').position().left);
                if(yearSelector.find('.bs-selected').attr('value') == -1){
                    daySelector.mouseleave();
                    yearSelector.mouseenter();
                } else if (monthSelector.find('.bs-selected').attr('value') == -1){
                    daySelector.mouseleave();
                    monthSelector.mouseenter();
                } else {
                    var year = yearSelector.find('.bs-selected').attr('value');
                    var month = monthSelector.find('.bs-selected').attr('value');
                    var days = monthDays[month - 1];
                    if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0 ){
                        if(month == 2){
                            days = 29;
                        }
                    }
                    if(daySelector.find('.bs-selected').attr('value') > days){
                        daySelector.find('.bs-selected').attr('value', -1);
                        daySelector.find('.bs-selected').html('选择日期');
                    }
                    daySelector.find('.bs-item a').show();
                    for(var i=days; i<31; i++){
                        daySelector.find('.bs-item a:eq(' + i + ')').hide();
                    }
                }
                daySelector.find('.bs-item a').each(function(index, item) {
                    $(item).unbind('click.rio').bind('click.rio', function(event) {
                        var day = $(item).attr('value');
                        daySelector.find('.bs-selected').attr('value', day);
                        daySelector.find('.bs-selected').html(day);
                        daySelector.find('.bs-list').hide();
                    });
                });
            }).bind('mouseleave', function(event){
                $(this).removeClass('bs-hover');
                $(this).find('.bs-selected').css('zIndex', 1);
                $(this).find('.bs-list').hide();
                yearSelector.mouseleave();
                monthSelector.mouseleave();
            });
            
            // 设置options
            $self._setOptions($self.options);
        },
        _setOption : function(key, value) {
            var $self = this;
            switch (key) {
            case 'date':
                var arr = value.split('-');
                var year = parseInt(arr[0]);
                var month = parseInt(arr[1]);
                var day = parseInt(arr[2]);
                
                this.yearSelector.find('.bs-selected').attr('value', year).html(year);
                this.monthSelector.find('.bs-selected').attr('value', month).html(month);
                this.daySelector.find('.bs-selected').attr('value', day).html(day);
                break;
            default:
                break;
            }
            return this;
        }
    });
})(jQuery);
